package com.xlf.service.judge.io.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xlf.common.core.exception.BizException;
import com.xlf.pojo.base.ProblemCase;
import com.xlf.service.judge.io.Resource;
import com.xlf.service.judge.mapper.ProblemCaseMapper;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * @Author xlf 2734029211@qq.com
 * @Date 2023/9/18 15:02
 * @Description: TODO 从数据库加载测试用例文件
 * @Version 1.0
 */
public class MysqlResource implements Resource {

    private Long problemId;

    public MysqlResource(Long problemId){
        Assert.notNull(problemId,() -> new BizException("problemId 不能为空!"));
        this.problemId  = problemId;
    }

    @Override
    public List<ProblemCase> getCase()  {
        ProblemCaseMapper problemCaseMapper = SpringUtil.getBean(ProblemCaseMapper.class);
        List<ProblemCase> problemCases = problemCaseMapper.selectList(new LambdaQueryWrapper<ProblemCase>()
                .eq(ProblemCase::getPid, problemId)
                .orderByAsc(ProblemCase::getSeq,ProblemCase::getId));
        Assert.notNull(problemCases,() -> new BizException(problemId + "的测试用例在数据库为空!"));
        return problemCases;
    }
}
